<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var student = {
        sno: "0001234",
        sname: "王老五",
        sex: "男",
        tel: null,
        score: [100, 99, 98, 90],
        course: {
          id: "001",
          cname: "H5网页设计",
        },
      };

      //  封装深克隆的方法
      function deepCloneObj(data) {
        var newData = data.constructor === Array ? [] : {};
        for (var key in data) {
          // 判断值是否为null，因为null的类型是Object
          if (data[key] === null) {
            newData[key] = null;
          } else {
            // 判断是否为引用类型
            if (typeof data[key] === "object") {
              //如果是引用类型用深拷贝
              newData[key] = deepCloneObj(data[key]);
            } else {
              //如果是基本类型的数据用浅拷贝
              newData[key] = data[key];
            }
          }
        }
        return newData;
      }

      var newStudent = deepCloneObj(student);
      console.log(newStudent);
      // 修改克隆数据
      newStudent.score.push(100);
      newStudent.sname = "猪猪头";
      console.log("修改后克隆的数据", newStudent);
      console.log("原始的数据", student);
    </script>
  </body>
</html>
